<?php 
session_start();
require_once ("class/m_return.php");
require_once ("class/m_stock.php");
$m = new M_return();
$m_stock=new M_stock();

if (isset($_POST['m'])  &&  $_POST['m']!=""){
	if ($_POST['m']== 'view'){
		if(isset($_POST['id']) && $_POST['id']!=""){ 
			$id=$_POST['id'];
			$type=$_POST['type']; //s:send , c:check , v:view
			
			if ($type=='c' || $type=='cl'){
				$col=9;
			}else{
				$col=7;
			}
			
			$page=isset($page)?$page:1;
			
			$resultHeader =$m->ViewHeader($id);
			
			$result_detail=$m->ViewDetail($id);
 						
			$rows=$m->getRowCount();
			
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows>0){
				while($row = $result_detail->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";    
					}
					$html .= getTableDetail($row,$i,$type)."</tr>";
					$i++;
				}
			}else{
					$html="<tr>"
					."<td colspan='".$col."' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'header'=> json_encode($resultHeader),
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo  json_encode( $ar_result );
			
		}
	 	//******************************
		//****       Confirm return product
		//******************************			
		}else if ($_POST['m']== 'confirm'){
			$return_id=$_POST['re_id'];
			$list= $_POST['var'];//{'id': return_id_detail ,'pid':p_id, 'amt': amt ,'type':type}
			$branchID=$_POST['branch'];
			$ar_count=count($list);
			$user=$_SESSION['username'];
			//loop update
			$script="";
			 foreach ($list as $val) {
				
				$script.=$m->UpdateReturnProduct($val['id'],$val['amt'],$val['type']);
				
				//Remove stock branch
				$m_stock->UpdateReturnStockBranch($val['pid'],$branchID,$val['amt'],$user,0);
				//1:Normal , 2:Defect
				if ($val['type']==1){
					///Add stock to HO
					$m_stock->UpdateReturnStockBranch($val['pid'], $_SESSION['bid'],$val['amt'],$user,1);
				}else{
					//update stock to table defect
					$m_stock->UpdateReturnStockDefect($val['pid'],$branchID,$val['amt'],$user);
				}
			 }
			 
			$m->UpdateStatustoClose($return_id, $user );
			
			echo "completed";
		
		//******************************
		//****  update after checking
		//******************************			
		}else if ($_POST['m']== 'send'){
			$id=$_POST['id'];

			$sqlupdate="Update tb_return_product "
							." set Status=2 "
							.", ReturnDate=Now() "
							.", ReturnBy='".$_SESSION['username'] ."'"
							." Where ID=".$id;					 
			
			 $m->Excute($sqlupdate);
			 
			 //Send SMS
			// $s=new SMS('0866586950','Return Product','THAIBULKSMS');
			// $s->SendSMS();
			// $s=null;
			// //Send Email
			// $to = "jinnavo@gmail.com";
			// $subject = "Return product";
			// $message = "Return product. ";
			// $from = "jinnavo@hotmail.com";
			// $headers = "From:" . $from;
			// mail($to,$subject,$message,$headers);
			echo "completed";
		}
}else{
			echo "error"	;
	}

	function getTableDetail($row,$i,$type){
		$html .="<td align='right'>".$i."</td>";
		$html .="<td style='display:none'>".$row['ID']."</td>";
		$html .="<td style='display:none'>".$row['ProductID']."</td>";
		$html .="<td align='left'>".$row['ProductName']."</td>";
		$html .="<td  align='left'>".$row['CatName']."</td>";
		$html .="<td  align='center'>".$row['ProductType']."</td>";
		$html .="<td  align='center'>".$row['Unit']."</td>";
		$html .="<td  align='right'>". number_format( $row['Amounts_s'],'0','.',',')."</td>";
		$html .="<td  align='center'>".$row['ReturnType_s']."</td>";
		//Type c is confirm return 
		 if ($type=='c'){
			$html .="<td  align='center'><input type='text'  style='text-align:right;' class='amtBox'  id='txtAmt' name='txtAmt' maxlength='4' size='5' value=".number_format( $row['Amounts_s'],'0','.',',')."></input></td>";
			$html .="<td  align='center'><select id='type_conf' name='type_conf'  class='amtBox'>";
			if ($row['typeid_s']=="1")
				$html .="<option value='1'  selected >Normal</option><option value='2' >Defect</option>";
			else 
				$html .="<option value='1' >Normal</option><option value='2' selected >Defect</option>";
				
			$html .="</select></input></td>";
			
		 }else if ($type=='cl'){
			$html .="<td  align='right'>". number_format( $row['Amounts_r'],'0','.',',')."</td>";
			$html .="<td  align='center'>".$row['ReturnType_r']."</td>";
		 }
		return $html;	
	}
?>